package com.linsen.core.mybatisplus.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linsen.core.common.util.ConvertUtil;

import java.util.List;

/**
 * @author zhaoyong
 * @date 2023/3/22
 */
public final class PageUtil {
    /**
     * 类型转换
     *
     * @param <T>
     * @return
     */
    public static <T> Page<T> convert(Page<?> sourcePage, Class<T> targetClass) {
        if (ObjectUtil.isNull(sourcePage)) {
            return null;
        }

        Page<T> targetPage = new Page<>();
        if (sourcePage.getTotal() == 0) {
            return targetPage;
        }

        List<T> list = ConvertUtil.convert(sourcePage.getRecords(), targetClass);
        BeanUtil.copyProperties(sourcePage, targetPage);
        targetPage.setRecords(list);

        return targetPage;
    }
}
